package com.tfswx.ruleengine.database.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Ricky
 * @date 2021/7/6
 */
@Slf4j
@Configuration
public class DataSourceConfig {
    @Resource
    private DataSource dataSource;

    private static String databaseId = "mysql";

    public static String getDatabaseId() {
        return databaseId;
    }

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() throws SQLException {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties p = new Properties();
        p.setProperty("Oracle", "oracle");
        p.setProperty("MySQL", "mysql");
        p.setProperty("SQLite", "sqlite");
        p.setProperty("PostgreSQL", "postgresql");
        p.setProperty("DM DBMS", "dm");
        p.setProperty("SQL Server", "sqlserver");
        databaseIdProvider.setProperties(p);
        databaseId = databaseIdProvider.getDatabaseId(dataSource);
        if (databaseId == null) {
            databaseId = new VendorDatabaseIdProvider().getDatabaseId(dataSource);
        }
        log.info("数据库厂商标识：{}", databaseId);
        return databaseIdProvider;
    }

    //    /**
    //     * Mapper接口所在包名，Spring会自动查找其下的Mapper
    //     *
    //     * @return mapperScannerConfigurer
    //     */
    //    @Bean
    //    public MapperScannerConfigurer mapperScannerConfigurer() {
    //        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    //        mapperScannerConfigurer.setBasePackage("**.dao");
    //        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    //
    //        return mapperScannerConfigurer;
    //    }
}
